function Weather_UDP_Server_Rx(block)
%*=+--+=#=+--      UAS Severe Weather Simulation Softeware        --+=#=+--+=#*%
%          Copyright (C) 2013 Regents of the University of Colorado.           %
%                             All Rights Reserved.                             %
%                                                                              %
%    This program is free software: you can redistribute it and/or modify      %
%    it under the terms of the GNU General Public License version 2 as         %
%    published by the Free Software Foundation.                                %
%                                                                              %
%    This program is distributed in the hope that it will be useful,           %
%    but WITHOUT ANY WARRANTY; without even the implied warranty of            %
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             %
%    GNU General Public License for more details.                              %
%                                                                              %
%    You should have received a copy of the GNU General Public License         %
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.     %
%                                                                              %
%           Jack Elston                                                        %
%           elstonj@colorado.edu                                               %
%                                                                              %
%*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*%
	setup(block);

function setup(block)
	block.NumDialogPrms = 0;

	block.NumInputPorts = 1;
	block.NumOutputPorts = 1;

	block.SetPreCompInpPortInfoToDynamic;
  block.SetPreCompOutPortInfoToDynamic;

  block.InputPort(1).Complexity   = 'Real';
  block.InputPort(1).DataTypeId   = 0;
  block.InputPort(1).SamplingMode = 'Sample';
  block.InputPort(1).Dimensions   = 1;

  block.OutputPort(1).Complexity   = 'Real';
  block.OutputPort(1).DataTypeId   = 6;
  block.OutputPort(1).SamplingMode = 'Sample';
  block.OutputPort(1).Dimensions   = 5;

	block.SampleTimes = [1/1000 0]; % how do I get varables from the workspace?

	block.RegBlockMethod('PostPropagationSetup',    @DoPostPropSetup);
	block.RegBlockMethod('Start',                   @Start);  
	block.RegBlockMethod('Outputs',                 @Outputs);
	block.RegBlockMethod('Terminate',               @Terminate);

function DoPostPropSetup(block)
	block.NumDworks = 1;

	block.Dwork(1).Name            = 'state';
	block.Dwork(1).Dimensions      = 1;
	block.Dwork(1).DatatypeID      = 0;      % double
	block.Dwork(1).Complexity      = 'Real'; % real
	block.Dwork(1).UsedAsDiscState = true;

function Start(block)
	block.Dwork(1).Data = 0;

function Outputs(block)
	udp = block.InputPort(1).Data;
	persistent lat lon alt time hdg

	if block.Dwork(1).Data == 0
		lat = 0;
		lon = 0;
		alt = 0;
		time = -1000;
		hdg = 0;
		block.Dwork(1).Data = 1;
	end

	config_type  = 0;
	request_type = 1;

	try,
		len = pnet(udp,'readpacket','noblock');
    if len > 0
			type = pnet(udp,'read',1,'uint8');
			switch type
				case {config_type}
				case {request_type}
					lat = pnet(udp,'read',1,'int32');
					lon = pnet(udp,'read',1,'int32');
					alt = pnet(udp,'read',1,'int32');
					time = pnet(udp,'read',1,'int32');
					hdg = pnet(udp,'read',1,'int32');
			end
		end
	end

	block.OutputPort(1).Data(1) = lat;
	block.OutputPort(1).Data(2) = lon;
	block.OutputPort(1).Data(3) = alt;
	block.OutputPort(1).Data(4) = time;
	block.OutputPort(1).Data(5) = hdg;

function Terminate(block)
